<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// TODO 15/11/2009 
// * Check order of new configs. OK. Check later.
// * Provide proper section work. OK. Check later.
// * Provide admin interface for configs. PARTLY OK. 

// TODO 6/12/2009 
// * Config editing should work. Put Config_id into session.
// * Add checkboxes for mass actions at admin configs' panel.
// * Check work of filter at admin configs' panel.
// * Create static function for quick getting of configs.

class Configurationmodel extends Model {

	var $cid = null;
	var $name = null;
	var $group = null;
	var $title = null;
	var $form_control = null;
	var $default_value = null;
	var $values = null;
	var $section = null;
	var $section_comment = null;
	var $section_style = null;
	var $order = null;
	var $required = null;

	var $object_id = null;
	var $configs = array();

	function __construct ()
	{
		parent::Model();
		$this->load->database();
	}

	function get ()
	{
		$this->db->select('configuration.*, configuration_groups.name AS `group_name`, configuration_groups.title AS `group_title`');
		$this->db->from('configuration');
		$this->db->join('configuration_groups', 'configuration.group_id = configuration_groups.cgid', 'INNER');
		if ($this->cid || $this->name) {
			if ($this->cid) { $this->db->where('cid', $this->cid); }
			else { $this->db->where('name', $this->name); }
			$query = $this->db->get(null, 1)->result();
			if (count($query)) {
				
				$this->cid = $query[0]->cid;
				$this->name = $query[0]->name;
				$this->default_value = $query[0]->default_value;
				$this->title = $query[0]->title;
				$this->group_id = $query[0]->group_id;
				$this->form_control = $query[0]->form_control;
				$this->values = $query[0]->values;
				$this->section = $query[0]->section;
				$this->section_comment = $query[0]->section_comment;
				$this->section_style = $query[0]->section_style;
				$this->order = $query[0]->order;
				$this->required = $query[0]->required;
				$this->section_title = $query[0]->section_title;
				$this->object_id = null;
				
				$this->configs = array();
				return true;
			} else {
				return false;
			}
		}

		$this->db->join('object_configuration', 'configuration.cid = object_configuration.cid' . (!empty($this->object_id) ? ' AND object_configuration.oid = ' . $this->object_id : ''), 'LEFT');
		$this->db->select('object_configuration.value');
		$this->db->where("configuration_groups.name", $this->group);
		$this->db->order_by("configuration.section", 'ASC');
		$this->db->order_by("configuration.order", 'ASC');
		
		$results = $this->db->get(null)->result();
		if (count($results)) {
			foreach ($results as $config) { $this->configs[$config->name] = $config; }
		}
		return count($this->configs);
	}
	
	
	function count_configs ($filter) {
		$this->db->from('configuration');
		
		if (is_array($filter)) {
			foreach ($filter as $key => $value) {
				if (empty($value)) { continue; }
				if ($key == 'group_title') { $this->db->like('configuration_groups.title', $value); }
				elseif ($key == 'group_name') { $this->db->like('configuration_groups.name', $value); }
				else {
					if(is_numeric($value)) { $this->db->where('configuration.' . $key, $value); }
					else { $this->db->like('configuration.' . $key, $value);  }
				}
			}
		}
		
		$this->db->select('COUNT(*) AS `ccount`');
		
		if (!empty($filter['group_title']) || !empty($filter['group_title'])) { 
			$this->db->join('configuration_groups', 'configuration.group_id = configuration_groups.cgid', 'INNER');
		}
		
		$res = $this->db->get()->result();
		return $res[0]->ccount;
	}
	
	function getConfigs($filter, $start, $limit) {
		
		if (is_array($filter)) {
			foreach ($filter as $key => $value) {
				if (empty($value)) { continue; }
				if ($key == 'group_title') { $this->db->like('configuration_groups.title', $value); }
				else { 
					if(is_numeric($value)) { $this->db->where('configuration.' . $key, $value); }
					else { $this->db->like('configuration.' . $key, $value);  }
				}
			}
		}
		
		$this->db->select('configuration.*, configuration_groups.name AS `group_name`, configuration_groups.title AS `group_title`');
		$this->db->from('configuration');
		$this->db->join('configuration_groups', 'configuration.group_id = configuration_groups.cgid', 'INNER');
		$this->db->order_by('configuration.section', 'ASC');
		$this->db->order_by('configuration.order', 'ASC');
		
		return $this->db->get(null, $start, $limit)->result();
	}
	
	
	function admin () {
		if ($this->cid || $this->name || $this->object_id) { return $this->get(); }
		
		$select = 'configuration.*';
		$this->db->from('configuration');
		if ($this->group) {
			
			$this->db->join('configuration_groups', 'configuration.group_id = configuration_groups.cgid', 'INNER');
			
			if (is_int($this->group)) { $this->db->where('group_id', $this->group); }
			else {
				$select .= ', configuration_groups.name AS `group_name`, configuration_groups.title AS `group_title`';
				$this->db->where('configuration_groups.name', $this->group); 
			}
		}
		
		if ($this->form_control) {}
		if ($this->section) {}
		if ($this->required) {}

		$this->db->select($select);
		$results = $this->db->get('configuration')->result(null);
		return $results;
	}
	
	
	function checkOrder () {
		$this->db->select('order + 1 AS `order`');
		$this->db->from('configuration');
		$this->db->order_by('configuration.order', 'DESC');
		
		$section_where_apx = $this->section ? " AND configuration.section = '" . $this->section . "'" : '';
		
		if(is_numeric($this->group)) {
			$this->db->where('group_id = ' . $this->group . $section_where_apx);
		} else {
			$this->db->join('configuration_groups', 'configuration_groups.cgid = configuration.group_id', 'INNER');
			$this->db->where('configuration_groups.name = ' . $this->group . $section_where_apx);
		}
		
		$res = $this->db->get(null, 1)->result();
		if (count($res)) { 
			return $this->order = $res[0]->order; }
		else { return 1; }
	}
	

	function save () {
		if (!count($this->configs)) {
			
			if ($this->new_group) {
			
				$save_group = array(
					'name' => $this->new_group,
					'title' => $this->group_title,
					'comment' => $this->group_comment
				);
				$this->db->insert('configuration', $save_group);
				$this->group = $this->db->insert_id;
			}
			
			$save = array(
				'name' => $this->name,
				'title' => $this->title,
				'values' => $this->values,
				'default_value' => $this->default_value,
				'form_control' => $this->form_control,
				'group_id' => $this->group,
				'section' => $this->section,
				'section_title' => $this->section_title,
				'section_comment' => $this->section_comment,
				'section_style' => $this->section_style,
				'order' => $this->order,
				'required' => $this->required);
			
			if ($this->cid) {
				return $this->db->update('configuration', $save, array('cid', $this->cid));
			} else {
				return $this->db->insert('configuration', $save);
			}
		} else {
			$res = false;
			foreach ($this->configs as $config) {
				
				$this->db->select('cid');
				$this->db->from('object_configuration');
				$this->db->where('oid', $this->object_id);
				$this->db->where('cid', $config->cid);
				$exists = $this->db->get(null, 1)->num_rows;
				
				$save = array(
					'oid' => $this->object_id, 
					'cid' => $config->cid, 
					'value' => $config->value
				);
				
				$this->db->where('oid', $this->object_id);
				$this->db->where('cid', $config->cid);
				
				if ($exists) { $res = $this->db->update('object_configuration', $save); }
				else { $res = $this->db->insert('object_configuration', $save); }
			}
			return $res;
		}
	}
	
	function delete() {
		if ($this->cid) {
			$this->db->where("id", $this->cid);
			return $this->db->delete("configuration");
		}
	}
	
	function getObjectConfigs () {
		$this->get();
		return $this->configs;
	}
	
	function getGroups () {
		
		$this->db->select('cgid, title');
		$this->db->from('configuration_groups');
		$get = $this->db->get();
		$fetch = $get->result();
		$groups = array('' => '');
		if ($get->num_rows) {
			foreach ($fetch as $item) {
				$groups[$item->cgid] = $item->title;
			}
		}
		
		return $groups;
	}
	
	function getSections ($group) {
		
		$this->db->select('section, section_title, configuration_groups.title AS `group_titile`');
		$this->db->from('configuration');
		$this->db->join('configuration_groups', 'configuration_groups.cgid = configuration.group_id', 'INNER');
		$this->db->group_by('section');
		$this->db->where('section IS NOT NULL');
		$fetch = $this->db->get()->result();

		$sections = array('' => '');
		if (count($fetch)) {
			foreach ($fetch as $item) {
				$sections[$item->group_titile][$item->section] = $item->section_title ? $item->section_title : $item->section;
			}
		}
		return $sections;
	}
}
